﻿Imports System.Data
Imports System.Data.SqlClient
Public Class DatabaseConnect
    Private cn As String = "Server = DV-PC\SQLExpress; Database = QuanLyNam_DoAnCK_LTQLUD1; Integrated Security = True"
    Private conn As SqlConnection
    Private cmd As SqlCommand

    Public Function Connect() As Boolean
        conn = New SqlConnection(cn)
        Try
            conn.Open()
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show("Connection failed. View details : " + ex.Message)
        End Try
        Return True
    End Function

    Public Sub Close()
        If conn.State = ConnectionState.Open Then
            conn.Close()
        End If
        conn.Dispose()
    End Sub

    Public Function GetData(ByVal sql As String) As DataTable
        Dim dt As New DataTable
        Connect()
        cmd = New SqlCommand(sql, conn)
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dt)
        Close()
        Return dt
    End Function

    Public Function Execute(ByVal sql As String)
        cmd = New SqlCommand(sql, conn)
        Connect()
        cmd.Connection() = conn
        cmd.CommandText = sql
        Return cmd.ExecuteNonQuery()
    End Function
End Class
